﻿window.__uri = function (uri) {
    return uri;
};
window.global = {};
/**
 配置文件
 **/
$.module('SYS.Config', function () {
    console.log("welcome")
    var root = '/web/';
    return {
        mode: "develop",
        dataRoot: "/",
        main: root,
        version: "20170405",
        title: '管理系统',
        desc: '响应式、美观',
        //apiRoot: "http://test.api.zsmy.cn/" // 测试环境：http://test.api.zsmy.cn/ 生产环境：http://api.zsmy.cn/ 修改为动态获取

    };
});
/**
 核心部分
 **/
$.module('SYS.Core', function () {

    return {
        /**
         * Helper function to parse the user agent.  Sets the following
         * .os.webkit
         * .os.android
         * .os.ipad
         * .os.iphone
         * .os.webos
         * .os.touchpad
         * .os.blackberry
         * .os.opera
         * .os.fennec
         * @api private
         */
        os: (function () {
            var os = {},
                userAgent = navigator.userAgent,
                type = "";
            os.webkit = userAgent.match(/WebKit\/([\d.]+)/) ? true : false;
            os.android = userAgent.match(/(Android)\s+([\d.]+)/) || userAgent.match(/Silk-Accelerated/) ? true : false;
            os.ipad = userAgent.match(/(iPad).*OS\s([\d_]+)/) ? true : false;
            os.iphone = !os.ipad && userAgent.match(/(iPhone\sOS)\s([\d_]+)/) ? true : false;
            os.webos = userAgent.match(/(webOS|hpwOS)[\s\/]([\d.]+)/) ? true : false;
            os.touchpad = os.webos && userAgent.match(/TouchPad/) ? true : false;
            os.ios = os.ipad || os.iphone;
            os.blackberry = userAgent.match(/BlackBerry/) || userAgent.match(/PlayBook/) ? true : false;
            os.opera = userAgent.match(/Opera Mobi/) ? true : false;
            os.fennec = userAgent.match(/fennec/i) ? true : false;
            os.desktop = !(os.ios || os.android || os.blackberry || os.opera || os.fennec);

            for (var key in os) {
                if (os[key]) {
                    type += key + ",";
                }
            }
            if (os.android || os.ipad || os.iphone || os.blackberry) {
                os.mobile = true;
            }
            SYS.Core.os = os;
            os.type = type;
            return os;
        }()),

        /** 封装ajax请求 **/
        ajax: function (param) {
            var that = this;
            var common = param.common;
            common["uid"] = $.cookie("u_id");
            $.ajax({
                url: '/' + param.url,
                type: "post",
                dataType: 'json',
                data: {
                    parameters: JSON.stringify({
                        common: common,
                        data: param.data
                    })
                },
                beforeSend: function () {
                    that.checkLogin();
                },
                success: function (data) {//客户端jquery预先定义好的callback函数,成功获取跨域服务器上的json数据后,会动态执行这个callback函数
                    param.success(data);
                },
                complete: function () {
                    that.closeLoading();
                }
            });
        },
        /** ajax跨域访问，使用 jsonp  **/
        ajaxJsoup: function (param) {
            var that = this;
            var common = param.common;
            $.ajax({
                url: that.getDomain() + param.url,
                dataType: 'jsonp',
                data: {
                    parameters: JSON.stringify({
                        common: common,
                        data: param.data
                    })
                },
                jsonp: 'callback',
                jsonpCallback: "success_jsonpCallback",
                success: function (data) {
                    param.success(data);
                }
            });
        },
        ajaxAsync: function (param) {
            var common = param.common;
            $.ajax({
                url: '/' + param.url,
                type: "post",
                dataType: 'json',
                async: false,    //同步
                data: {
                    parameters: JSON.stringify({
                        common: common,
                        data: param.data
                    })
                },
                success: function (data) {
                    param.success(data);
                }
            });
        },
        checkLogin: function () {
            var is_login = $.cookie("is_login");  //判断是否登录
            if (is_login != "true") {
                location.href = "../../login.html";
            }
        },
        showMenuHead: function () {
            var url = window.location.href;
            if(url.indexOf('?') > -1) {
                url = url.substr(0, url.indexOf('?'));
            }
            if (url.indexOf('platform_user_detail.html') >  -1) {
                return;
            }
            var items = url.split('#');
            if(items && items.length == 2) {
                var menu_id = items[1];
                if(menu_id){
                    $.ajax({
                        url: '/sys/showMenuHead',
                        type: "post",
                        dataType: 'json',
                        data: {
                            parameters: JSON.stringify({
                                common: {},
                                data: {id: menu_id}
                            })
                        },
                        success: function (data) {
                            if(data.code == 1){
                                var location = data.data.location;
                                $(".wrapper-content .ibox-title h5").prepend(location + ' | ');

                                if(data.data.help && data.data.help.length > 0) {
                                    var help = data.data.help;
                                    help = help.replace(new RegExp("\n","g"), '<br/>');
                                    var html = '';
                                    html += '<div class="pull-right" style="margin-right: 10px;">';
                                    html += '<div class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-question-circle"> 帮助</i></div>';
                                    html += '<ul class="dropdown-menu dropdown-menu-user" style="margin: 21px 0; padding:10px; line-height: 30px; max-width: 300px; ">';
                                    html += '帮助说明:<br/>';
                                    html += help;
                                    html += '</ul>';
                                    html += '</div>'
                                    $(".wrapper-content .ibox-title").append('<div class="ibox-tools">' + html + '</div>');
                                }
                            }
                        }
                    });
                }
            }
        },
        closeLoading: function () {
            $(".table-loading").remove();
        },
        loadEvent: function () {
            var that = this;
            //表格增加等待层
            $("table").each(function () {
                var loading = '' +
                    '<div class="spiner-example table-loading">' +
                    '   <div class="sk-spinner sk-spinner-three-bounce">' +
                    '       <div class="sk-bounce1"></div>' +
                    '       <div class="sk-bounce2"></div>' +
                    '       <div class="sk-bounce3"></div>' +
                    '   </div>' +
                    '</div>';
                $(this).before(loading);
            });

            //点击搜索框，表格显示加载中
            $("#btn_search").on('click', function () {
                $("#myTable").bootstrapTable('showLoading');
            });

            $('.popover-toggle').popover();
            //加载页面菜单导航
            that.showMenuHead();

            //延迟加载图片
            //图片代码：<img class="lazy" data-original="img/1.jpg" />
            //div背景图<div class="lazy lazy-bg" data-original="img/1.jpg"></div>
            $("div.lazy,img.lazy").lazyload({
                threshold : 200,
                effect : "fadeIn", //使用淡入特效
                failure_limit : 10
            });
        },
        getDomain: function () {
            var that = this;
            var domain = '';
            that.ajaxAsync({
                url: 'sys/getConfig',
                common:{},
                data:{},
                success: function (data) {
                    if(data.code == 1) {
                        domain = data.data.api_domain + "/";
                    }
                }
            });
            return domain;
        },
        getMDomain: function () {
            var that = this;
            var domain = '';
            that.ajaxAsync({
                url: 'sys/getConfig',
                common:{},
                data:{},
                success: function (data) {
                    if(data.code == 1) {
                        domain = data.data.m_domain + "/";
                    }
                }
            });
            return domain;
        },
        getNumberValue: function (obj) {
            var dlt = 0;
            if (obj && !isNaN(obj)) {
                return obj;
            }
            return dlt;
        },
        isEmpty: function (value, trim) {
            return value === undefined || value === null || value.length === 0 || (trim && $.trim(value) === '');
        },
    };
});
SYS.Core.loadEvent();